package com.css.zfzx.data.common;

import com.css.zfzx.data.config.ProvinceCityAreaCode;
import com.css.zfzx.data.primary.basedivision.repository.BaseDivisionRepository;
import com.css.zfzx.data.primary.basedivision.repository.entity.DivisionEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component
public class ProvinceCityAreaUtil {

    @Autowired
    private BaseDivisionRepository baseDivisionRepository;

    private static final List<ProvinceCityAreaCode> pcaList = new ArrayList<>();

    /*@Autowired
    public ProvinceCityAreaUtil(BaseDivisionRepository baseDivisionRepository) {
        this.baseDivisionRepository = baseDivisionRepository;
    }*/

    public List<ProvinceCityAreaCode> getAdminAreaCodes() {
        if (pcaList.size() != 0) {
            return pcaList;
        }
        initProvinceCityArea(baseDivisionRepository.findAllDivision());
        return pcaList;
    }

    /**
     * 将所有省市县放在同一对象中
     *
     */
    private void initProvinceCityArea(List<DivisionEntity> divisionList) {
        //查询数据库
        for (DivisionEntity division : divisionList) {
            ProvinceCityAreaCode provinceCityAreaCode = new ProvinceCityAreaCode();
            provinceCityAreaCode.setCode(division.getDivisionCode());
            String[] split = division.getAllParentId().split(",");
            if (split.length == 2) {
                //省
                provinceCityAreaCode.setProvince(division.getDivisionName());
            } else if (split.length == 3) {
                //市
                provinceCityAreaCode.setProvince(getCityName(split[1], divisionList));
                provinceCityAreaCode.setCity(division.getDivisionName());
            } else if (split.length == 4) {
                //县
                provinceCityAreaCode.setProvince(getCityName(split[1], divisionList));
                provinceCityAreaCode.setCity(getCityName(split[2], divisionList));
                provinceCityAreaCode.setArea(division.getDivisionName());
            }
            pcaList.add(provinceCityAreaCode);
        }

    }

    private String getCityName(String id, List<DivisionEntity> list) {
        Optional<DivisionEntity> match = list.stream().filter(d -> id.equals(d.getDivisionId())).findAny();
        return match.map(DivisionEntity::getDivisionName).orElse(null);
    }

    public String getName(String code) {
        String name = null;
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        for (ProvinceCityAreaCode pca : pcaList) {
            if (code.equals(pca.getCode())) {
                if (code.contains("0000")) {
                    name = pca.getProvince();
                    break;
                } else if (code.contains("00")) {
                    name = pca.getCity();
                    break;
                } else {
                    name = pca.getArea();
                    break;
                }
            }
        }
        return name;
    }

}
